package com.changgou.seckill.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.pojo.SeckillStatus;
import com.changgou.seckill.service.SeckillOrderService;
import com.changgou.utils.TokenDecode;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "SeckillOrderController")
@RestController
@RequestMapping("/seckillOrder")
@CrossOrigin
public class SeckillOrderController {

    @Autowired
    private SeckillOrderService seckillOrderService;


    @Autowired
    private TokenDecode tokenDecode;

    /****
     * URL:/seckill/order/add
     * 添加订单
     * 调用Service增加订单
     * 匿名访问：anonymousUser
     * @param time
     * @param id
     */
    @RequestMapping(value = "/add")
    public Result add(String time, Long id) throws ExecutionException, InterruptedException {
            //用户登录名
            String username = tokenDecode.getUserInfo().get("username");

            //调用Service增加订单
            Boolean bool = seckillOrderService.add(id, time, username);

            if (bool) {
                //抢单成功
                return new Result(true, StatusCode.OK, "抢单成功！");
            }

        return new Result(true, StatusCode.ERROR, "服务器繁忙，请稍后再试");
    }


    @RequestMapping("/query")
    public Result query(String status) {
        //用户登录名
        String username = tokenDecode.getUserInfo().get("username");
        List<SeckillStatus> seckillStatusList = seckillOrderService.queryStatus(username, status);
        if (seckillStatusList != null && seckillStatusList.size() > 0) {
            return new Result(true, StatusCode.OK, "用户查询订单", seckillStatusList);
        }
        return new Result(false, StatusCode.NOTFOUNDERROR, "无用户订单信息");
    }


}
